﻿using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Common.Util;
using DevExpress.XtraEditors;
using Dewin.BO.AgentMember;
using Log.Controller;
using Tabber.Util;

namespace Tabber.Forms
{
    public partial class AgentManagement : XtraForm
    {
        public AgentManagement()
        {
            InitializeComponent();
            SetText();
        }

        #region Events
        /// <summary>
        /// Dong form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Mo chuong trinh
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDone_Click(object sender, EventArgs e)
        {
            Hide();
            var mainForm = new MainForm();
            mainForm.Show();
        }
        #endregion

        #region Chuc nang load
        /// <summary>
        /// Su kien Load form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AgentManagement_Load(object sender, EventArgs e)
        {
            // Gan ham delegate de sau moi lan selected value cua combobox language change thi change text
            cboLanguage.ReSetText = SetText;
        }

        /// <summary>
        /// Ham set text theo ngon ngu
        /// </summary>
        private void SetText()
        {
            try
            {
                Text = TabberCommon.GetString("AgentManagement");
                colId.Caption = TabberCommon.GetString("Id");
                colUsername.Caption = TabberCommon.GetString("Username");
                colAddress.Caption = TabberCommon.GetString("Address");
                colType.Caption = TabberCommon.GetString("AgentType");
                colMemberCount.Caption = TabberCommon.GetString("MemberCount");
                colCheckbox.Caption = TabberCommon.GetString("Choose");
                btnAdd.Text = TabberCommon.GetString("Add");
                btnDelete.Text = TabberCommon.GetString("Delete");
                btnDone.Text = TabberCommon.GetString("Done");
                btnClose.Text = TabberCommon.GetString("Close");
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }

        /// <summary>
        /// Goi ham load data moi khi active form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AgentManagement_Activated(object sender, EventArgs e)
        {
            // Goi ham lay danh sach agent va fill vao grid
            GetAgentInfo();
        }

        /// <summary>
        /// Lay danh sach agent va bind vao grid
        /// </summary>
        private void GetAgentInfo()
        {
            try
            {
                var list = Agent.GetAccounts();
                gridAgents.DataSource = list.Select(agent => new
                {
                    agent.Id,
                    agent.Username,
                    agent.Type,
                    agent.Address,
                    MemberCount = agent.Members.Count
                });
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }
        #endregion

        #region Chuc nang edit Agent
        /// <summary>
        /// Su kien click vao nut edit thong tin agent, mo ra form edit thong tin
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void editAgent_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            try
            {
                var button = (ButtonEdit)sender;
                var thread = new Thread(OpenEditAgentForm);
                thread.Start(new[] { button.Text });
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }

        /// <summary>
        /// Mo form Edit agent
        /// </summary>
        /// <param name="agentId"></param>
        private void OpenEditAgentForm(object agentId)
        {
            try
            {
                var childForm = new AgentEdit { AgentId = agentId == null ? string.Empty : ((object[])agentId)[0].ToString() };
                Invoke((MethodInvoker)(() => childForm.ShowDialog()));
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }

        /// <summary>
        /// Mo form add agent
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                var thread = new Thread(OpenEditAgentForm);
                thread.Start();
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }
        #endregion

        #region Key
        private void AgentManagement_KeyDown(object sender, KeyEventArgs e)
        {
        }
        #endregion
    }
}